package com.zjuh.qss.biz.ao.impl;

import java.io.File;
import java.util.Date;
import java.util.List;

import wint.lang.utils.CollectionUtil;
import wint.lang.utils.DateUtil;
import wint.tools.redis.DefaultRedisClient;
import wint.tools.redis.RedisClient;
import wint.tools.similar.DefaultSimilarManager;
import wint.tools.similar.SimilarItem;
import wint.tools.similar.SimilarManager;
import wint.tools.similar.content.ContentResource;
import wint.tools.similar.store.TermCountStore;
import wint.tools.similar.store.fs.FsContentTermStore;
import wint.tools.similar.store.redis.RedisTermCountStore;

import com.zjuh.qss.biz.ao.SimilarWorkAO;
import com.zjuh.qss.biz.dao.query.SimilarTagQuery;
import com.zjuh.qss.biz.dao.similar.SimilarItemDAO;
import com.zjuh.qss.biz.dao.similar.SimilarTagDAO;
import com.zjuh.qss.biz.domain.similar.SimilarItemDO;
import com.zjuh.qss.biz.domain.similar.SimilarTagDO;

public class SimilarWorkAOImpl extends BaseAO implements SimilarWorkAO {
	
	private SimilarManager similarManager;
	
	private ContentResource taobaoItemContentResource;
	
	private SimilarTagDAO similarTagDAO;
	
	private SimilarItemDAO similarItemDAO;
	
	private String redisHost;
	
	private RedisClient redisClient;
	
	private int namespace = 1;
	
	private String basePath;
	
	private int count = 5;
	
	public void init() {
		DefaultRedisClient redisClient = new DefaultRedisClient();
		redisClient.setRedisHosts(redisHost);
		redisClient.init();
		
		this.redisClient = redisClient;
		
		FsContentTermStore fsContentResource = new FsContentTermStore(new File(basePath));
		TermCountStore termCountStore = new RedisTermCountStore(redisClient, namespace);
		similarManager = new DefaultSimilarManager(fsContentResource, termCountStore);
		
		log.warn("SimilarWorkAO init success.");
	}
	
	public void destroy() {
		if (redisClient != null) {
			redisClient.close();
		}
	}
	
	public void runDocSimilars() {
		log.warn("start runDocSimilars.");
		String groupId = getGroupId();
		
		// 预处理资源
		similarManager.processResource(groupId, taobaoItemContentResource);
		
		proccessWithText(groupId);
		log.warn("finish runDocSimilars.");
	}
	
	
	private void proccessWithText(String groupId) {
		log.warn("start proccessWithText.");
		int page = 1;
		while (true) {
			SimilarTagQuery tagQuery = new SimilarTagQuery();
			tagQuery.setPageNo(page);
			tagQuery.setPageSize(100);
			
			List<SimilarTagDO> tags = similarTagDAO.queryForQuery(tagQuery);
			if (CollectionUtil.isEmpty(tags)) {
				break;
			}
			
			for (SimilarTagDO tag : tags) {
				String inputText = tag.getIntputText();
				List<SimilarItem> similarItems = similarManager.findTopSimilars(inputText, groupId, count);
				for (SimilarItem similarItem : similarItems) {
					SimilarItemDO similarItemDO = new SimilarItemDO();
					similarItemDO.setTagId(tag.getId());
					similarItemDO.setSimilarValue(similarItem.getSimilarValue());
					similarItemDO.setItemId(Long.parseLong(similarItem.getId()));
					similarItemDAO.create(similarItemDO);
				}
				
				log.warn("tag:" + tag.getName() + "["+ tag.getId() +"]" + " has been finish. ");
			}
			
			page++;
		}
		log.warn("proccessWithText finished.");
	}
	
	private String getGroupId() {
		return DateUtil.formatDate(new Date(), "yyyy-MM-dd_HH_mm");
	}

	public void setTaobaoItemContentResource(ContentResource taobaoItemContentResource) {
		this.taobaoItemContentResource = taobaoItemContentResource;
	}

	public void setRedisHost(String redisHost) {
		this.redisHost = redisHost;
	}

	public void setNamespace(int namespace) {
		this.namespace = namespace;
	}

	public void setCount(int count) {
		this.count = count;
	}

	public void setBasePath(String basePath) {
		this.basePath = basePath;
	}

	public void setSimilarItemDAO(SimilarItemDAO similarItemDAO) {
		this.similarItemDAO = similarItemDAO;
	}
	

	public void setSimilarTagDAO(SimilarTagDAO similarTagDAO) {
		this.similarTagDAO = similarTagDAO;
	}

	
}
